﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using ZLDC.Common.Estate;
using ZLDC.Topo.Model.Estate;

namespace ZLDC.DAL.Estate
{
                public class HousingEverydayDAL
    {
                                                                                        public static void p_housingEveryday_insert(int UserId, string User, int housingId, int dltFlag, out string outmsg, out int newid, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@UserId", UserId));
            parms.Add(new SqlParameter("@User", User));
            parms.Add(new SqlParameter("@housingId", housingId));
            parms.Add(new SqlParameter("@dltFlag", dltFlag));

            SqlParameter pOutmsg = new SqlParameter("@outmsg", SqlDbType.VarChar, 256);
            pOutmsg.Direction = ParameterDirection.Output;
            parms.Add(pOutmsg);

            SqlParameter pNewid = new SqlParameter("@newid", SqlDbType.Int, 4);
            pNewid.Direction = ParameterDirection.Output;
            parms.Add(pNewid);

            db.execute_procedure("p_housingEveryday_insert", parms);

            outmsg = (pOutmsg.Value == System.DBNull.Value) ? "" : Convert.ToString(pOutmsg.Value);
            newid = (pNewid.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pNewid.Value);

        }

                                                                                public static DataTable p_housingEveryday_list_table(int page, int pagesize, string condition, string orderByName, string orderByType, ref int total, _Database db)
        {
            List<SqlParameter> parms = new List<SqlParameter>();

            parms.Add(new SqlParameter("@page", page));
            parms.Add(new SqlParameter("@pagesize", pagesize));
            parms.Add(new SqlParameter("@condition", condition));
            parms.Add(new SqlParameter("@orderByName", orderByName));
            parms.Add(new SqlParameter("@orderByType", orderByType));

            SqlParameter pTotal = new SqlParameter("@total", SqlDbType.Int, 4);
            pTotal.Direction = ParameterDirection.InputOutput;
            pTotal.Value = total;
            parms.Add(pTotal);
            SqlParameter pRetval = new SqlParameter("@retval", SqlDbType.Int, 4);
            pRetval.Direction = ParameterDirection.ReturnValue;
            parms.Add(pRetval);

            DataTable dt = null;
            dt = db.GetDataTable("p_housingEveryday_list", parms);

            total = (pTotal.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pTotal.Value);

            int retval = (pRetval.Value == System.DBNull.Value) ? 0 : Convert.ToInt32(pRetval.Value);

            return dt;
        }

                public static DataSet sql_housingEveryday_list_table(string sql, _Database db)
        {

            DataSet ds = new DataSet();
            ds = db.Query(sql);

            return ds;
        }

                                                        public static int HousingEverydayDelete(int id, _Database db)
        {
            int rows = 0;

            try
            {
                                string sql = " update t_housingEveryday set dltflag='1' where id=" + id + " ";

                rows = db.ExecuteSql(sql);
            }
            catch { }
            finally { db.close(); }

            return rows;
        }

        public static int HousingEverydayRecommendDelete(int id, _Database db)
        {
            int rows = 0;

            try
            {
                                string sql = " update t_housingEveryday set dltflag='1' where housingId=" + id + " and type=1 ";

                rows = db.ExecuteSql(sql);
            }
            catch { }
            finally { db.close(); }

            return rows;
        }
                public static int InsertHousingEveryDay(int UserId, string User, int housingId, string content, _Database db)
        {
            int rows = 0;
            try
            {
                string sql = "insert into t_housingEveryday(UserId,[User],housingId,addDate, dltFlag, type, content) values('" + UserId + "','" + User + "','" + housingId + "',GETDATE(),0,1,'" + content + "')";
                rows = db.ExecuteSql(sql);
            }
            catch { }
            finally
            {
                db.close();
            }
            return rows;
        }
                public static int SelectHousingEveryDayByHid(int housingid, int type, _Database db)
        {
            int rows = 0;
            try
            {
                string sql = "select * from t_housingEveryday where housingid=" + housingid + " and type=" + type + " and dltflag=0 ";
                DataSet ds = db.Query(sql);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    rows = ds.Tables[0].Rows.Count;
                }
            }
            catch { }
            finally
            {
                db.close();
            }
            return rows;
        }
        public static HousingEverydayInfo SelectHousingEveryDaysByHid(int housingid, int type, _Database db)
        {
            HousingEverydayInfo info = new HousingEverydayInfo();

            try
            {
                string sql = "select * from t_housingEveryday where housingid=" + housingid + " and type=" + type + " and dltflag=0 ";
                DataSet ds=null;

                ds = db.Query(sql);
                 if (ds == null)
                 {
                     return null;
                 }
                 else 
                 {
                     info.UserId =_Convert.ToInt(ds.Tables[0].Rows[0]["userid"].ToString());
                 }
            }
            catch { }
            finally
            {
                db.close();
            }
            return info;
        }
                public static int SelectHousingByUserId(int userid, _Database db)
        {
            int rows = 0;
            try
            {
                string sql = "select * from t_housingEveryday where UserId=" + userid + " and addDate between '" + DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00" + "' and '" + DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59" + "' and type=1 and dltflag=0";

                DataSet ds = db.Query(sql);
                rows = ds.Tables[0].Rows.Count;
            }
            catch
            {

            }
            finally
            {
                db.close();
            }

            return rows;
        }


    }
}
